<?php

namespace app\common\model;


use think\Model;
use traits\model\SoftDelete;

class Ask extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    protected $name = 'ask';

    /**
     * [scenicAsk 提问列表]
     * @Author   ZhouYunpeng
     * @DateTime 2020-12-25
     * @param    [type]      $scenic_id [description]
     * @return   [type]                 [description]
     */
    public static function scenicAsk($scenic_id)
    {
        $list = self::with('answer')
                ->where(['scenic_id' => $scenic_id,'status'=>'normal'])
                ->field('id,user_id,scenic_id,content,create_time')
                ->order('create_time desc')
                ->limit(4)
                ->select();
		$list = collection($list)->toArray();

        foreach ($list as &$v) {
            $v['create_time'] = date('Y-m-d H:i:m', $v['create_time']);
            foreach ($v['answer'] as &$item) {
                $item['create_time'] = date('Y-m-d H:i:m', $item['create_time']);
                //用回答的最新时间替换问题的最新时间
                if($item['create_time'] > $v['create_time']) {
                    $v['create_time'] = $item['create_time'];
                }
            }
            if (reset($v['answer'])) {
                $answer = array_shift($v['answer']);
                $v['answer_count'] = count($v['answer'])+1;//剩余元素的数量
                $v['answer'] = $answer;
            } else {
                $v['answer'] = null;
                $v['answer_count'] = 0;
            }
        }
        //数组重新排序
        $sort = array_column($list, "create_time");
        array_multisort($sort, SORT_DESC, $list);

        $data['ask_count'] = count($list);
        $data['ask_list'] = $list;
        return $data;
    }

    /***
     * @param array $where
     * 留言板列表
     */
    public function list($where = [], $page)
    {   
        $list = self::with('answer')
                ->where($where)
                ->field('id,user_id,scenic_id,content,create_time')
                ->order('create_time desc')
                ->page($page, 10)
                ->select();
        $list = collection($list)->toArray();
        foreach ($list as &$v) {
			$v['create_time'] = date('Y-m-d H:i:m', $v['create_time']);
            foreach ($v['answer'] as &$item) {
                $item['create_time'] = date('Y-m-d H:i:m', $item['create_time']);
                //用回答的最新时间替换问题的最新时间
                if($item['create_time'] > $v['create_time']) {
                    $v['create_time'] = $item['create_time'];
                }
            }
            if (reset($v['answer'])) {
                $answer = array_shift($v['answer']);
                $v['answer_count'] = count($v['answer'])+1;//剩余元素的数量
                $v['answer'] = $answer;
            } else {
                $v['answer'] = null;
                $v['answer_count'] = 0;
            }
        }
        //数组重新排序
        $sort = array_column($list, "create_time");
        array_multisort($sort, SORT_DESC, $list);
        return $list;

    }

    /**
     * [answerList 某一个问题下的所有问题]
     * @Author   ZhouYunpeng
     * @DateTime 2020-12-25
     * @param    array       $where [description]
     * @param    [type]      $page  [description]
     * @return   [type]             [description]
     */
    public function answerList($where = [], $page)
    {
        $answer_list = self::with('answer')
                    ->where($where)
                    ->field('id,user_id,scenic_id,content')
                    ->order('create_time desc')
                    ->page($page, 10)
                    ->find();
        $answer_list = $answer_list->toArray();
        foreach ($answer_list['answer'] as &$item) {
            $item['create_time'] = date('Y-m-d H:i:m', $item['create_time']);
        }
        return $answer_list;
    }


    /**
     * [answer 关联留言板下的所有回答]
     * @Author   ZhouYunpeng
     * @DateTime 2020-12-25
     * @return   [type]      [description]
     */
    public function answer()
    {   
        $list = $this->hasMany('Answer', 'ask_id')
            ->alias('a')
            ->join('user b', 'a.user_id = b.id')
            ->where('a.status','normal')
            ->field('a.id,ask_id,user_id,like,answer_text,images,a.create_time,username,avatar')
            ->order('create_time desc');
        return $list;
    }


    /**
     * [scenic 留言的景区]
     * @Author   ZhouYunpeng
     * @DateTime 2020-12-25
     * @param    [type]      $scenic_id [description]
     * @return   [type]                 [description]
     */
    public function scenic($scenic_id)
    {
       $scenicModle = new Scenic();
       $info = $scenicModle
                ->where('id',$scenic_id)
                ->field('scenic_name,scenic_images,total_sales')
                ->find();
        $image = explode(',',$info['scenic_images']);
        $info['scenic_image'] = reset($image);
        
        $info['mark'] = $scenicModle->mark($scenic_id);

        return $info;
    }

}